package com.hrt.web.comments.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.Comments;
import com.hrt.web.comments.dao.CommentsWebDao;

@Repository
public class CommentsWebDaoImpl extends BaseDaoImpl<Comments> implements CommentsWebDao{
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Comments> findDelAll() {
		return getHibernateTemplate().find("FROM Comments WHERE isdelete=0");
	}
	
	/**分页查询的重写*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Comments> queryPageByGiftId(List conditions,
			int pageIndex, int pageSize, String rankIdOption) {
		String hql = "select comm from Comments comm";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				hql = hql + " where comm." + field + " in(" + val+")";
			} else {
				if("1".equals(rankIdOption)){
					hql = hql + " and comm." + field + "=" + val+" and comm.isAddComment=0 ORDER BY comm.contentDet DESC,comm.commentTime DESC";
				}else{
					hql = hql + " and comm." + field + "=" + val+" and comm.isAddComment=0 ORDER BY comm.commentTime DESC";
				}
//				hql = hql + " and comm." + field + "=" + val+" and comm.isAddComment=0 ORDER BY id DESC";
			}
		}
		List<Comments> results = super.queryByPage(hql, pageIndex, pageSize);
		return results;
	}
	
	/**追评的分页*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Comments> queryAddCommById(List conditions,
			int pageIndex, int pageSize) {
		String hql = "select comm from Comments comm";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				hql = hql + " where comm." + field + " in(" + val+")";
			} else {
//				hql = hql + " and comm." + field + "=" + val+" and comm.isAddComment=1 ORDER BY id DESC";
				hql = hql + " and comm." + field + "=" + val+" and comm.isAddComment=1 ORDER BY comm.contentDet DESC,comm.commentTime DESC";
			}
		}
		List<Comments> results = super.queryByPage(hql, pageIndex, pageSize);
		return results;
	}

	/**根据礼品规格Id查找评论Id*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Long> findCommIdByGdeId(String gdeIds) {
		return getHibernateTemplate().find("SELECT  gcomm.id FROM Comments gcomm WHERE gcomm.isdelete=0 AND gcomm.gdeId IN ("+gdeIds+")");
	}
	
	/**追评   --》根据礼品规格Id查找评论*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Comments> findAddCommIdByGdeId(String gdeIds) {
		return getHibernateTemplate().find("SELECT gcomm FROM Comments gcomm WHERE gcomm.isdelete=0 AND gcomm.isAddComment=1 AND gcomm.gdeId IN ("+gdeIds+")");
	}
	
	/**图片   --》根据礼品规格Id查找有图片评论*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Comments> findPicCommIdByGdeId(String gdeIds) {
		return getHibernateTemplate().find("SELECT gcomm FROM Comments gcomm WHERE gcomm.isdelete=0 AND gcomm.picture!=null AND gcomm.picture!='' AND gcomm.gdeId IN ("+gdeIds+")");
	}

	/**根据规格id获取数量*/
	@Override
	public Long findNumByGdeId(String gdeIds) {
		return (long) getHibernateTemplate().find("select comm from Comments comm where comm.id in("+gdeIds+") and comm.isAddComment=0 and comm.isdelete=0 ORDER BY id DESC").size();
	}
	
	/**追评   --》根据规格id获取数量*/
	@Override
	public Long findAddCommNumByGdeId(String gdeIds) {
		return (long) getHibernateTemplate().find("select comm from Comments comm where comm.id in("+gdeIds+") and comm.isdelete=0 and comm.isAddComment=1 ORDER BY id DESC").size();
	}

	/**根据会员id礼品id和订单id去查找评论，判断这个订单是不是已经评论过了*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Comments> finNumByMemIdAndGdeId(Long memberid, Long gdeId, Long orderId) {
		
		return getHibernateTemplate().find("select comm from Comments comm where comm.hrtMemberId ="+memberid+" and comm.gdeId="+gdeId+" and comm.orderId="+orderId+"and comm.isdelete=0");
	}
	
	/**根据会员id礼品id和订单id去查找追评*/
	//ORDER BY comm.contentDet DESC,comm.commentTime DESC	//按照好评排序再按照时间排序
	@SuppressWarnings("unchecked")
	@Override
	public List<Comments> finCommByMemIdAndGdeId(Long hrtMemberId, Long gdeId, Long orderId) {
		
		return getHibernateTemplate().find("select comm from Comments comm where comm.hrtMemberId ="+hrtMemberId+" and comm.gdeId="+gdeId+" and comm.orderId="+orderId+" and comm.isAddComment=1 and comm.isdelete=0 ORDER BY commentTime ASC");
	}
	
	/**根据追评的会员id礼品id和订单id去查找初评*/
	@Override
	public Comments finCommByAddComm(Long hrtMemberId, Long gdeId, Long orderId){
		
		return (Comments) getHibernateTemplate().find("select comm from Comments comm where comm.hrtMemberId ="+hrtMemberId+" and comm.gdeId="+gdeId+" and comm.orderId="+orderId+" and comm.isAddComment=0 and comm.isdelete=0 ORDER BY commentTime ASC").get(0);
	}
}
